package com.leps.nq.bu.controller;

import com.leps.nq.bu.entity.BuTeam;
import com.leps.nq.bu.service.IBuTeamService;
import com.leps.nq.bu.vo.CreateTeamVo;
import com.leps.nq.bu.vo.TeamVo;
import com.leps.nq.bu.vo.UpdateTeamVo;
import com.leps.nq.common.aspect.Log;
import com.leps.nq.common.enums.BusinessType;
import com.leps.nq.common.vo.R;
import com.leps.nq.common.vo.RBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 任务队伍表 前端控制器
 * </p>
 *
 * @author hex
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/bu/bu-team")
@Api(tags = "任务队伍管理")
public class BuTeamController {

    @Autowired
    private IBuTeamService teamService;

    @ApiOperation("创建任务队伍但不分配队伍成员")
    @PostMapping
    @Log(title = "队伍", businessType = BusinessType.INSERT)
    public R<?> create(@RequestBody BuTeam team) {
        boolean save = this.teamService.save(team);
        return RBuilder.ofBoolean(save);
    }

    @ApiOperation("创建任务队伍并分配队伍成员")
    @PostMapping("/create-with-members")
    @Log(title = "队伍", businessType = BusinessType.INSERT)
    public R<?> create(@RequestBody CreateTeamVo createTeamVo) {
        boolean success = this.teamService.saveWithMembers(createTeamVo);
        return RBuilder.ofBoolean(success);
    }

    @GetMapping
    @ApiOperation("获取任务队伍列表")
    public R<List<TeamVo>> list() {
        List<TeamVo> list = this.teamService.listVo();
        return RBuilder.ok(list);
    }

    @GetMapping("/{teamId}")
    @ApiOperation("获取任务队伍及成员ID列表")
    public R<TeamVo> get(@PathVariable Integer teamId) {
        TeamVo vo = this.teamService.getTeamAndMemberInfo(teamId);
        return RBuilder.ok(vo);
    }

    @DeleteMapping
    @ApiOperation("删除任务队伍")
    @Log(title = "队伍", businessType = BusinessType.DELETE)
    public R<?> remove(@RequestBody List<Integer> teamIds) {
        boolean b = this.teamService.removeBatchByIds(teamIds);
        return RBuilder.ofBoolean(b);
    }

    @PutMapping
    @ApiOperation("修改任务队列并重新分配队伍成员")
    @Log(title = "队伍", businessType = BusinessType.UPDATE)
    public R<?> update(@RequestBody UpdateTeamVo vo) {
        boolean b = this.teamService.updateTeamAndMember(vo);
        return RBuilder.ofBoolean(b);
    }
}
